<?php
// +----------------------------------------------------------------------
// | najing [ 通用后台管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.najingquan.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 救火队队长
// +----------------------------------------------------------------------

namespace app\cardeadmin\controller;

use app\common\controller\AdminController;
use app\common\model\CodeLog;
use app\common\model\ServerLss;
use app\common\model\ServerCd;
use think\facade\Cache;
use think\facade\Db;
use think\facade\View;

/**
 * 平台设置
 * Class Setting
 * @package app\admin\controller
 */
class ProfitList extends AdminController
{
    /**
     * 平台设置
     * @author 救火队队长
     */
    public function index()
    {
        if($this->request->isPost()) {
            $data = input('param.');
            $model = new ServerCd();
            return $model->tableDataAdmin($data);
        }
        return View::fetch();
    }

    //银1拒付超额处理
    public function CdMaxLog()
    {
        if($this->request->isPost()) {
            $data = input('param.');
            $model = new ServerCd();
            return $model->tableDataAdminMax($data);
        }
        return View::fetch();
    }

    /**
     * 管理员处理拒付超额
     * @return array|\think\response\Json
     * @throws \Exception
     */
    public function editAction()
    {
        if($this->request->isPost()) {
            $data         = input('param.');
            $action_id    = $data['action_id'];
            $bank_type    = $data['bank_type'];
            $up_status    = $data['up_status'];
            if($bank_type == 1){
                $listModel = new ServerCd();
            }else{
                $listModel = new ServerLss();
            }
            $list  = $listModel::find($action_id);
            $blog = true;
            Db::startTrans();
            try {
                $user_id        = $list['user_id'];
                $decline_type   = $list['type'];
                $card_num       = $list['card_num'];
                $cl_ctime       = getUtcTime();
                if($up_status == 1){
                    $server_amount = ($decline_type == 1) ? 0.2 : 0.3;
                }else{
                    $server_amount = $list['server_amount'];
                }

                $user_list          = Db::name('user')->where(['id'=>$user_id])->find();
                $user_balance       = $user_list['balance'];
                $user_balance_end   = $user_balance - $server_amount;
                $user_balance_end   = getSprintf(round($user_balance_end,2));

                $card_num64 = substr($card_num , 0 ,6) . '***' . substr($card_num , -4);
                $desc = "拒付服务费{{$card_num64}}";

                Db::name('user')->where(['id'=>$user_id])->update(['balance'=>$user_balance_end]);

                Db::name('user_cash_log')->save([
                    'user_id'       => $user_id,
                    'type'          => 1,
                    'action_type'   => 3,
                    'card_type'     => substr($card_num , 0 ,6),
                    'amount'        => $server_amount,
                    'balance'       => $user_balance_end,
                    'desc'          => $desc,
                    'ctime'         => $cl_ctime,
                    'bank'          => $bank_type
                ]);
                if($bank_type == 1){
                    Db::name('server_cd')->where(['id'=>$action_id])
                        ->update([
                            'is_action'     =>1,
                            'server_amount' => $server_amount,
                            'cl_ctime'      => $cl_ctime
                        ]);
                }else{
                    Db::name('server_lss')->where(['id'=>$action_id])
                        ->update([
                            'is_action'     =>1,
                            'server_amount' => $server_amount,
                            'cl_ctime'      => $cl_ctime
                        ]);
                }
                Db::commit();
            } catch (\Exception $e) {
                $blog = false;
                $error_msg = $e->getMessage();
                echo $error_msg;
                Db::rollback();
            }
            if($blog){
                return $this->scs('Confirm the successful.');
            }else{
                return $this->scs('Confirm the failed.');
            }
        }

        $action_id = input('get.action_id');
        $bank_type = input('get.bank_type');
        if($bank_type == 1){
            $dataModel = new ServerCd();
        }else{
            $dataModel = new ServerLss();
        }
        $list_info = $dataModel->where(['id' => $action_id])->find();
        View::assign([
            'list'   => $list_info,
            'action_id' => $action_id,
            'bank_type' => $bank_type
        ]);
        View::layout(false);
        return [
            'status'    => 1,
            'datas'     => View::fetch()
        ];
    }

    public function lssLog()
    {
        if($this->request->isPost()) {
            $data = input('param.');
            $model = new ServerLss();
            return $model->tableDataAdmin($data);
        }
        return View::fetch();
    }

    /**
     * 银3拒付超额处理
     * @return array|mixed|string
     * @throws \think\db\exception\DbException
     */
    public function lssMaxLog()
    {
        if($this->request->isPost()) {
            $data = input('param.');
            $model = new ServerLss();
            return $model->tableDataAdminMax($data);
        }
        return View::fetch();
    }

    /**
     * 货转
     * @return array|mixed|string
     * @throws \think\db\exception\DbException
     */
    public function codeLog()
    {
        if($this->request->isPost()) {
            $data = input('param.');
            $model = new CodeLog();
            return $model->tableDataAdmin($data);
        }
        $amount_sum = Db::name('code_log')->where(['is_action' => 1])->sum('amount');
        View::assign([
            'amount_sum'     => $amount_sum
        ]);
        return View::fetch();
    }
}